package com.habo.controller;

import com.google.gson.Gson;
import com.habo.entity.Comment;
import com.habo.entity.Share;
import com.habo.entity.User;
import com.habo.service.CommentService;
import com.habo.service.ShareService;
import com.habo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by junbb on 2017/3/27.
 */
@Controller
public class ShareController {
    @Resource
    private ShareService shareService;

    @Resource
    private UserService userService;

    @Resource
    private CommentService commentService;

    @RequestMapping(value = "discover")
    public String discover() {
        return "discover";
    }

    @RequestMapping(value = "discoverServlert")
    public void discoverServlert(String op, HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        Gson gson = new Gson();
        if (op.equals("loadPage")) {
            List<Share> shares = shareService.shareFindByPage(001, 0, 3);
            List<User> u = new ArrayList<User>();
            for (Share s :
                    shares) {
                User user = new User("001", "chan", "123456", "male", "xm", "12580", "7640474@qq.com", "01.jpg", new Date());
                user.setShare(s);
                u.add(user);
            }
            out.print(gson.toJson(u));
            out.flush();
            out.close();
        } else if (op.equals("loadDiscuss")) {
            int sid = Integer.parseInt(request.getParameter("Sid"));
            List<Comment> comments = commentService.commentSelect(sid);
            out.print(gson.toJson(comments));
            out.flush();
            out.close();
        } else if (op.equals("transmit")) {
            String dcl_teta = request.getParameter("dcl_teta");
            System.out.println(dcl_teta);
        } else if (op.equals("getUM")) {
            User user = userService.findUserById("001");

        }
    }

    @RequestMapping(value = "test")
    public String tests(HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        List<Share> shares = shareService.shareFindByPage(001, 0, 3);
        List<User> u = new ArrayList<>();
        for (Share s :
                shares) {
            User user = new User("001", "chan", "123456", "male", "xm", "12580", "7640474@qq.com", "01.jpg", new Date());
            user.setShare(s);
            u.add(user);
        }
        Gson gson = new Gson();
        out.print(gson.toJson(u));
        out.flush();
        out.close();
        return "test";
    }

    @RequestMapping(value = "share")
    public void share(HttpServletRequest request, HttpServletResponse response) {
        String scontent;
        String shttp;
        String dcl_teta = request.getParameter("dcl_teta");
        String sfrom = request.getParameter("Sfrom");
        System.out.println(dcl_teta);
        System.out.println(sfrom);
        if (dcl_teta.contains("http:")) {
            scontent = dcl_teta.substring(0, dcl_teta.indexOf("http:"));
            shttp = dcl_teta.substring(dcl_teta.indexOf("http:"));
            Share share = new Share("001", scontent, shttp, new Date(), sfrom);
            shareService.shareInsert(share);
        }
    }
}